package com.yycx.module.bus.provider.handler.haiying.tasktodo.monitor;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 结算支付单监控
 *
 * @author zyf
 */
@Component
public class HySettlePayMonitorHandler implements PageInterceptor<BusPayment> {


    @Autowired
    private FlowableHelp flowableHelp;
    @Autowired
    private BusDeptService deptService;


    @Autowired
    private BusSalemanService salemanService;

    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        String beginDate = cq.getParams("beginDate");
        String saleManName = params.get("saleManName");
        String endDate = cq.getParams("endDate");
        String customId = cq.getParams("customId");
        String payeeName = cq.getParams("payeeName");
        cq.addSelect(BusSaleman.class, "saleManName", "saleManBusinessNo");
        cq.eq(BusPayment.class, "deptId");
        cq.eq(BusPayment.class, "companyId");
        cq.like(BusPayment.class, "paymentNo");
        cq.like("saleman.saleManName", saleManName);
        if (FlymeUtils.isNotEmpty(payeeName)) {
            String pinyin = PinyinUtil.getFirstLetter(payeeName, "").toUpperCase();
            cq.and(e -> e.like("payeeName", payeeName).or().likeRight("payeeName", pinyin));
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("amountDate", beginDate);
            cq.le("amountDate", endDate);
        }
        if (FlymeUtils.isNotEmpty(customId)) {
            cq.eq("customId", customId);
        }
        cq.in(true, "paymentState", 1,2);
        cq.eq("amountType", AmountTypeEnum.Settle.getCode());
        Long companyId = OpenHelper.getCompanyId();
        cq.eq("payment.companyId", companyId);
        cq.createJoin(BusSaleman.class);
        cq.orderByDesc("createTime");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        String companyName = OpenHelper.getAttr("companyName", String.class);
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("processInstanceId");
            EntityMap taskMap = flowableHelp.getTaskInfo(processInstanceId);
            if (FlymeUtils.isNotEmpty(taskMap)) {
                entityMap.putAll(taskMap);
            }
            Long deptId = entityMap.getLong("deptId");
            entityMap.put("companyName", companyName);
            if (FlymeUtils.isNotEmpty(deptId)) {
                BusDept dept = deptService.getById(deptId);
                entityMap.put("deptName", dept.getDeptName());
            }
            Long businessKey = entityMap.getLong("paymentId");
            if (FlymeUtils.isNotEmpty(businessKey)) {
                Integer paymentState = entityMap.getInt("paymentState");
                if (paymentState.equals(2)) {
                    entityMap.put("taskName", "已结算");
                }
            }
        }
        return result;
    }

}
